package cn.com.haopy.dataes.db.datasource;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@EnableTransactionManagement
@Configuration
@MapperScan("cn.com.haopy.dataes.db.mapper")
public class DataSourceConfig {
    @Autowired
    @Qualifier("mybatisPlusInterceptor")
    private MybatisPlusInterceptor mybatisPlusInterceptor;

    @Autowired
    @Qualifier("metaObjectHandler")
    private MetaObjectHandler metaObjectConfig;

    @Bean(name = "mainDatasource")
    @ConfigurationProperties(prefix = "spring.datasource.druid.main")
    public DataSource mainDatasource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "serviceDatasource")
    @ConfigurationProperties(prefix = "spring.datasource.druid.service")
    public DataSource serviceDatasource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean
    @Primary
    public DataSource multipleDataSource(@Qualifier("mainDatasource") DataSource main,
                                         @Qualifier("serviceDatasource") DataSource service) {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();

        Map<Object, Object> targetDataSource = new HashMap<>(2);
        targetDataSource.put(DBTypeEnum.MAIN.getValue(), main);
        targetDataSource.put(DBTypeEnum.SERVICE.getValue(), service);

        dynamicDataSource.setTargetDataSources(targetDataSource);
        dynamicDataSource.setDefaultTargetDataSource(main);

        return dynamicDataSource;
    }

    @Bean("sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(multipleDataSource(mainDatasource(), serviceDatasource()));

        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        bean.setMapperLocations(resolver.getResources("classpath*:mapper/*/**Mapper.xml"));

        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setCacheEnabled(false);
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setLazyLoadingEnabled(true);
//        configuration.setMultipleResultSetsEnabled(true);
//        configuration.setDefaultStatementTimeout(60);
//        configuration.setDefaultFetchSize(100);
//        configuration.setAutoMappingBehavior(AutoMappingBehavior.FULL);
//        configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);


        bean.setConfiguration(configuration);
        bean.setPlugins(mybatisPlusInterceptor);

        GlobalConfig globalConfig = new GlobalConfig();

        GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
        dbConfig.setIdType(IdType.AUTO);
        dbConfig.setLogicNotDeleteValue("0");
        dbConfig.setLogicDeleteValue("1");
        dbConfig.setLogicDeleteField("deleted");

        globalConfig.setDbConfig(dbConfig);
        globalConfig.setMetaObjectHandler(metaObjectConfig);

        bean.setGlobalConfig(globalConfig);

        return bean.getObject();
    }
}
